/*
 * @Description: 
 * @Author: Sun
 * @Date: 2019-08-19 21:41:29
 * @LastEditors: Sun
 * @LastEditTime: 2019-08-19 22:33:53
 */
// 防抖
export function _debounce(fn, delay) {

  var delay = delay || 200;
  var timer;
  return function () {
    var th = this;
    var args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(function () {
      timer = null;
      fn.apply(th, args);
    }, delay);
  };
}
// 节流
export function _throttle(fn, interval) {
  var last;
  var timer;
  var interval = interval || 200;
  return function () {
    var th = this;
    var args = arguments;
    var now = +new Date();
    if (last && now - last < interval) {
      clearTimeout(timer);
      timer = setTimeout(function () {
        last = now;
        fn.apply(th, args);
      }, interval);
    } else {
      last = now;
      fn.apply(th, args);
    }
  }
}
// 防抖222
export function fnThrottle(method, delay, duration) {
  var that = this;
  var timer = timer;
  var begin = new Date().getTime();
  return function () {
    var context = that;
    var args = arguments;
    var current = new Date().getTime();
    clearTimeout(timer);
    if (current - begin >= duration) {
      method.apply(context, args);
      begin = current;
    } else {
      console.log(delay)
      that.timer = setTimeout(function () {
        method.apply(context, args);
      }, delay);
    }
  }
}

///搜索截流函数
export function debounce(func, delay) {
  let timer

  return function (...args) {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      func.apply(this, args)
    }, delay)
  }
}


/**
 * 函数防抖
 */
export function de(fn, delay) {
  // 记录上一次的延时器
  var timer = null;
  var delay = delay || 200;
  return function () {
    var args = arguments;
    var that = this;
    // 清除上一次延时器
    clearTimeout(timer)
    timer = setTimeout(function () {
      fn.apply(that, args)
    }, delay);
  }
}

/**
 * 函数节流
 */
export function the(fn, delay) {
  var lastTime;
  var timer;
  var delay = delay || 200;
  return function () {
    var args = arguments;
    // 记录当前函数触发的时间
    var nowTime = Date.now();
    if (lastTime && nowTime - lastTime < delay) {
      clearTimeout(timer);
      timer = setTimeout(function () {
        // 记录上一次函数触发的时间
        lastTime = nowTime;
        // 修正this指向问题
        fn.apply(this, args);
      }, delay);
    } else {
      lastTime = nowTime;
      fn.apply(this, args);
    }
  }
} 

/**
 * @description [throttle 节流函数]
 * @author shanshuizinong
 * @param {Function} fn 延时调用函数
 * @param {Number} delay 延迟多长时间
 * @param {Number} atleast 至少多长时间触发一次
 * @return {Function} 延迟执行的方法
 */
// export function throttle(fn, delay, atleast) {
//   let timer = null
//   let previous = null
//   return function () {
//     let now = +new Date()
//     if (!previous) previous = now
//     if (atleast && now - previous > atleast) {
//       fn()
//       previous = now
//       clearTimeout(timer)
//     } else {
//       clearTimeout(timer)
//       timer = setTimeout(function () {
//         fn()
//         previous = null
//       }, delay)
//     }
//   }
// }
